package com.xiaoran.RMQ_1.test1;

import com.rabbitmq.client.*;

public class Consumer_one {
    public static final  String QUEUE_NAME="node2_queue";
    public static final  String  IP="192.168.171.132";
    //交换机的名称
    public static final  String  fed_exchange="fed_exchange";
    public static void main(String args[]) throws Exception{
        //1.创建连接工厂
        ConnectionFactory connectionFactory=new ConnectionFactory();
        //2 工厂IP连接RabbitMQ的队列
        connectionFactory.setHost(IP);
        //3 设置用户名
        connectionFactory.setUsername("admin");
        //4 设置密码
        connectionFactory.setPassword("123");
        //5 创建连接
        Connection connection=connectionFactory.newConnection();
        //6 创建信道
        Channel channel=connection.createChannel();

        //创建交换机的类型
        channel.exchangeDeclare(fed_exchange,BuiltinExchangeType.DIRECT);
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        channel.queueBind(QUEUE_NAME,fed_exchange,"routinfkey");
        //7.0 声明接受消息
       DeliverCallback delivercallback=(consumerTag,message)->{
         System.out.println(new String(message.getBody()));
       };
        //7.1 取消消息时回调
        CancelCallback cancelCallback=consumerTag->{
          System.out.println("消息消费被中断");
        };
                //7.2 接受消息
         channel.basicConsume(QUEUE_NAME,true,delivercallback,cancelCallback);
    }
}
